package com.viphuli.fragment;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.loopj.android.http.RequestParams;
import com.offroader.utils.JsonUtils;
import com.offroader.utils.StringUtils;
import com.offroader.utils.ViewUtils;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.common.Constants;
import com.viphuli.common.Constants.PayType;
import com.viphuli.common.MyPageHelper;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.part.PurchasePayTicket;

/**
 * 支付中心
 * 
 * @author li.li
 *
 */
public class PurchaseOrderPayFragment extends BaseProgressFragment {

	@Bind(R.id.id_main_content_service_company_order_name)
	protected TextView nameText;

	@Bind(R.id.id_main_content_service_company_order_price)
	protected TextView priceText;

	@Bind(R.id.id_main_content_service_company_order_user_name)
	protected TextView userNameText;

	@Bind(R.id.id_main_content_service_company_order_phone)
	protected TextView userPhoneText;

	@Bind(R.id.id_main_content_service_company_order_site)
	protected TextView siteText;

	@Bind(R.id.id_main_content_service_company_order_time)
	protected TextView timeText;

	@Bind(R.id.id_main_content_service_company_order_consume_ticket)
	protected TextView ticketText;

	@Bind(R.id.id_order_pay_weichat)
	protected ViewGroup weichatLayout;

	@Bind(R.id.id_order_pay_alipay)
	protected ViewGroup alipayLayout;

	@Bind(R.id.id_order_pay_offline)
	protected ViewGroup offlineLayout;

	@Bind(R.id.id_main_content_service_company_order_pay_price)
	protected TextView payPriceText;

	@Bind(R.id.id_order_consume_ticket_layout)
	protected ViewGroup ticketLayout;

	@Bind(R.id.id_main_content_service_company_order_pay)
	protected TextView orderPayBtn;

	private String orderId;
	private int payType;
	private int payPrice;
	private PurchasePayTicket ticket;
	private List<PurchasePayTicket> tickets;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_order_pay;
	}

	@Override
	protected void initParams() {

		Bundle args = getArguments();
		orderId = args.getString("order_id");
		payType = args.getInt("pay_type", PayType.weichatPay.getPayType());//被始化值

	}

	@Override
	protected void initView() {
	}

	@Override
	protected void initData() {

		//初始化页面数据
		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());

		if (token.isLogin()) {
			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());
			params.put("order_id", orderId);
			ApiRequest.purchaseOrderPayInfo.request(this, params, false);

		} else {
			AccountLoginFragment.goWithConfirm(getActivity());
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (resultCode == Activity.RESULT_OK) {

			if (requestCode == Constants.ACTIVITY_REQUEST_TICKET_CODE) {

				if (data != null) {

					String ticketJson = data.getStringExtra(AccountTicketFragment.KEY_PAY_SELECTED_TICKET);

					if (StringUtils.isNotBlank(ticketJson))
						ticket = JsonUtils.fromJson(ticketJson, PurchasePayTicket.class);

					//选择后赋值
					if (ticket == null) {

						ticketText.setText(caller.getString(R.string.str_ticket_empty));

					} else {

						ticketText.setText(ticket.getName());
						setTicket(ticket);

					}

					//重新计算付费金额
					initPayPrice();

				} else {

					ViewUtils.toast(R.string.tip_ticket_get_fails);

				}

			}

		}

	}

	@OnClick(R.id.id_order_consume_ticket_layout)
	public void myTickte() {
		Bundle params = new Bundle();
		String ticketJson = JsonUtils.toJson(tickets);
		params.putString("tickets", ticketJson);
		MyPageHelper.accountTicket.showMyPage(getActivity(), params, Constants.ACTIVITY_REQUEST_TICKET_CODE);

	}

	@OnClick({ R.id.id_order_pay_weichat, R.id.id_order_pay_alipay, R.id.id_order_pay_offline })
	public void choosePayType(View v) {

		if (R.id.id_order_pay_weichat == v.getId()) {

			ImageView weichatIv = (ImageView) weichatLayout.getChildAt(0);
			ImageView alipayIv = (ImageView) alipayLayout.getChildAt(0);
			ImageView offlineIv = (ImageView) offlineLayout.getChildAt(0);
			weichatIv.setImageResource(R.drawable.ic_order_buy_choose);
			alipayIv.setImageResource(R.drawable.ic_order_buy_unchoose);
			offlineIv.setImageResource(R.drawable.ic_order_buy_unchoose);

			payType = PayType.weichatPay.getPayType();

		} else if (R.id.id_order_pay_alipay == v.getId()) {

			ImageView weichatIv = (ImageView) weichatLayout.getChildAt(0);
			ImageView alipayIv = (ImageView) alipayLayout.getChildAt(0);
			ImageView offlineIv = (ImageView) offlineLayout.getChildAt(0);
			weichatIv.setImageResource(R.drawable.ic_order_buy_unchoose);
			alipayIv.setImageResource(R.drawable.ic_order_buy_choose);
			offlineIv.setImageResource(R.drawable.ic_order_buy_unchoose);

			payType = PayType.aliPay.getPayType();

		} else if (R.id.id_order_pay_offline == v.getId()) {

			ImageView weichatIv = (ImageView) weichatLayout.getChildAt(0);
			ImageView alipayIv = (ImageView) alipayLayout.getChildAt(0);
			ImageView offlineIv = (ImageView) offlineLayout.getChildAt(0);
			weichatIv.setImageResource(R.drawable.ic_order_buy_unchoose);
			alipayIv.setImageResource(R.drawable.ic_order_buy_unchoose);
			offlineIv.setImageResource(R.drawable.ic_order_buy_choose);

			payType = PayType.offlinePay.getPayType();

		}

	}

	@OnClick(R.id.id_main_content_service_company_order_pay)
	public void goToPay() {

		Bundle args = new Bundle();
		args.putString("order_id", orderId);
		args.putInt("pay_type", payType);

		PayType payTypeObj = PayType.getType(payType);

		//测试每次使用1分钱
		//		payPrice = 1;

		RequestParams params = new RequestParams();
		params.put("order_id", orderId);
		params.put("pay_type", payType);
		params.put("ticket_id", ticket == null ? 0 : ticket.getTicketId());
		params.put("pay_price", payPrice);

		if (PayType.weichatPay.equals(payTypeObj)) {
			
			ApiRequest.purchaseOrderPayWeichat.request(this, params, false);

		} else if (PayType.aliPay.equals(payTypeObj)) {

			ApiRequest.purchaseOrderPayAlipay.request(this, params, false);

		} else if (PayType.offlinePay.equals(payTypeObj)) {

			ApiRequest.purchaseOrderOffline.request(this, params, false);

		}

	}

	/**
	 * 获得支付金额
	 */
	public void initPayPrice() {
		AccessTokenKeeper token = AccessTokenKeeper.readAccessToken(getActivity());
		if (token.isLogin()) {
			RequestParams params = new RequestParams();
			params.put("uid", token.getOpenId());
			params.put("order_id", orderId);
			params.put("ticket_id", ticket == null ? 0 : ticket.getTicketId());
			ApiRequest.purchaseOrderPayPrice.request(this, params);

		} else {
			AccountLoginFragment.goWithConfirm(getActivity());
		}
	}

	/**
	 * 支付成功页面
	 */
	public void goToPayResultSuccess() {

		Bundle args = new Bundle();
		args.putString("order_id", orderId);
		args.putInt("pay_type", payType);
		MyPageHelper.purchasePaySuccess.showMyPage(getActivity(), args);

	}

	/**
	 * 支付失败页面
	 */
	public void goToPayResultFails() {

		Bundle args = new Bundle();
		args.putString("order_id", orderId);
		args.putInt("pay_type", payType);
		MyPageHelper.purchasePayFails.showMyPage(getActivity(), args);

	}

	public TextView getNameText() {
		return nameText;
	}

	public TextView getPriceText() {
		return priceText;
	}

	public TextView getUserNameText() {
		return userNameText;
	}

	public TextView getUserPhoneText() {
		return userPhoneText;
	}

	public TextView getSiteText() {
		return siteText;
	}

	public TextView getTimeText() {
		return timeText;
	}

	public TextView getTicketText() {
		return ticketText;
	}

	public ViewGroup getWeichatLayout() {
		return weichatLayout;
	}

	public ViewGroup getAlipayLayout() {
		return alipayLayout;
	}

	public ViewGroup getOfflineLayout() {
		return offlineLayout;
	}

	public TextView getPayPriceText() {
		return payPriceText;
	}

	public void setPayPrice(int payPrice) {
		this.payPrice = payPrice;
	}

	public void setTicket(PurchasePayTicket ticket) {
		this.ticket = ticket;
	}

	public void setTickets(List<PurchasePayTicket> tickets) {
		this.tickets = tickets;
	}

	public String getOrderId() {
		return orderId;
	}

	public int getPayType() {
		return payType;
	}

}
